package com.ykdl.member.kid.image;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

public class ImageUtil {
	public static Bitmap processCircleBitmap(Bitmap bitmap, float radius,
			float border) {
		int width = (int) (2.f * radius);
		Bitmap rounder = Bitmap.createBitmap(width, width,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(rounder);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0x00000000);
		canvas.drawRect(0, 0, width, width, paint);
		paint.setColor(0xff000000);
		canvas.drawCircle(radius, radius, radius - border, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		float x = bitmap.getWidth() / 2.f;
		float y = bitmap.getHeight() / 2.f;
		float temp = Math.min(x, y);
		canvas.drawBitmap(bitmap, new Rect((int) (x - temp), (int) (y - temp),
				(int) (x + temp), (int) (y + temp)), new Rect(0, 0, width,
				width), paint);
		paint.setXfermode(null);
		return rounder;
	}

	public static Bitmap processRoundRectBitmap(Bitmap bitmap, int width,
			int height, float radius) {
		Bitmap rounder = Bitmap.createBitmap(width, width,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(rounder);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0x00000000);
		canvas.drawRect(0, 0, width, width, paint);
		paint.setColor(0xff000000);
		canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius,
				paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

		float x = bitmap.getWidth() / 2.f;
		float y = bitmap.getHeight() / 2.f;
		float temp = Math.min(x, y);
		canvas.drawBitmap(bitmap, new Rect((int) (x - temp), (int) (y - temp),
				(int) (x + temp), (int) (y + temp)), new Rect(0, 0, width,
				width), paint);
		paint.setXfermode(null);
		return rounder;
	}

	// 通过Uri获取图片方向
	public static int getOrientation(ContentResolver contentResolver, Uri uri) {
		String path = getRealPathFromURI(contentResolver, uri);
		if (path == null) {
			return 0;
		}
		return getExifOrientation(path);
	}

	public static int getExifOrientation(String filepath) {
		int degree = 0;
		ExifInterface exif = null;
		try {
			exif = new ExifInterface(filepath);
		} catch (IOException ex) {
		}
		if (exif != null) {
			int orientation = exif.getAttributeInt(
					ExifInterface.TAG_ORIENTATION, -1);
			if (orientation != -1) {
				// We only recognize a subset of orientation tag values.
				switch (orientation) {
				case ExifInterface.ORIENTATION_ROTATE_90:
					degree = 90;
					break;
				case ExifInterface.ORIENTATION_ROTATE_180:
					degree = 180;
					break;
				case ExifInterface.ORIENTATION_ROTATE_270:
					degree = 270;
					break;
				}

			}
		}
		return degree;
	}

	public static void setExifOrientation(String filepath, int orientation) {
		ExifInterface exif = null;
		try {
			exif = new ExifInterface(filepath);
		} catch (IOException ex) {
		}
		if (exif != null) {
			exif.setAttribute(ExifInterface.TAG_ORIENTATION,
					String.valueOf(orientation));
		}
	}

	// 根据Uri获取文件路径
	public static String getRealPathFromURI(ContentResolver resolver,
			Uri contentUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		// 查询数据库
		Cursor cursor = resolver.query(contentUri, proj, null, null, null);
		if (cursor != null) {
			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			String result = cursor.getString(column_index);
			cursor.close();
			return result;
		} else {
			return null;
		}
	}

	public static boolean saveImage(Bitmap image, String filePath) {
		OutputStream output = null;
		try {
			output = new FileOutputStream(filePath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		boolean result = image.compress(CompressFormat.JPEG, 100, output);
		try {
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static Bitmap decodeFile(String filePath, ImageOptions options,
			String tag) {
		try {
			File f = new File(filePath);
			if (f.exists()) {
				BitmapFactory.Options _options = new BitmapFactory.Options();
				int width = ImageOptions.SIZE_ORIGINAL;
				int height = ImageOptions.SIZE_ORIGINAL;

				if (options != null) {
					width = options.getWidth();
					height = options.getHeight();
					if (width != ImageOptions.SIZE_ORIGINAL
							|| height != ImageOptions.SIZE_ORIGINAL) {
						_options.inJustDecodeBounds = true;
						// 获取图片原始大小
						BitmapFactory.decodeFile(filePath, _options);
						int imgWidth = _options.outWidth;
						int imgHeight = _options.outHeight;
						int sample = 1;
						// 计算横向采样率
						if (width != ImageOptions.SIZE_ORIGINAL) {
							float temp = (float) imgWidth / (float) width;
							sample = (int) temp;
							if (sample < 1) {
								sample = 1;
							} else if (temp - sample >= options.getRatio()) {
								sample += 1;
							}
						}
						// 计算竖向采样率
						if (width != ImageOptions.SIZE_ORIGINAL) {
							float temp = (float) imgHeight / height;
							int tempSample = (int) temp;
							if (tempSample < 1) {
								tempSample = 1;
							} else if (temp - tempSample >= options.getRatio()) {
								tempSample += 1;
							}
							sample = Math.max(sample, tempSample);
						}
						_options.inJustDecodeBounds = false;
						_options.inSampleSize = sample;
					}
				} else {
					_options.inJustDecodeBounds = false;
					_options.inSampleSize = 1;
				}
				// 获取图片
				return BitmapFactory.decodeFile(filePath, _options);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
	}
}
